package bioinfo.stats;

import bioinfo.*;
import javax.swing.*;
import java.awt.*;
import java.util.Properties;
import java.util.Hashtable;
import java.util.Enumeration;

import com.approximatrix.charting.swing.*;
import com.approximatrix.charting.model.*;
import com.approximatrix.charting.render.*;
import com.approximatrix.charting.*;

public class Third extends Stats {
    private Hashtable<String, Integer> hash_e;
    private Hashtable<String, Double> hash_length_e;
    private Hashtable<String, Integer> hash_a;
    private Hashtable<String, Double> hash_length_a;
    private Hashtable<String, Integer> hash_b;
    private Hashtable<String, Double> hash_length_b;
    private ChartPanel chart;
    private JPanel jp_charts;

    public Third() {
        hash_e = new Hashtable<String, Integer>();
        hash_length_e = new Hashtable<String, Double>();

        hash_a = new Hashtable<String, Integer>();
        hash_length_a = new Hashtable<String, Double>();

        hash_b = new Hashtable<String, Integer>();
        hash_length_b = new Hashtable<String, Double>();

        Hashtable<String, Integer> hash;
        Hashtable<String, Double> hash_length;
        for(ARN arn_tmp : Main.arns) {
            String anticodon = arn_tmp.getAnticodon();

            if(arn_tmp.getCateg().equals("Eukarya")) {
                hash = hash_e;
                hash_length = hash_length_e;
            }
            else if(arn_tmp.getCateg().equals("Archaea")) {
                hash = hash_a;
                hash_length = hash_length_a;
            }
            else {
                hash = hash_b;
                hash_length = hash_length_b;
            }

            Integer tmp = (hash.get(anticodon) == null) ? 0 : hash.get(anticodon);
            Double tmp_length = (hash_length.get(anticodon) == null) ? 0 : hash_length.get(anticodon);
            hash.put(anticodon, tmp+1);

            hash_length.put(anticodon, (((double)tmp_length*(double)tmp)+(double)arn_tmp.getSeq().length())/((double)tmp+1.));
        }


        // Create a MultiScatterDataModel
        MultiScatterDataModel model = new MultiScatterDataModel();

        model.addData(hashToArray(hash_e, hash_length_e), "Eukarya");

        model.setSeriesLine("Eukarya", false);
        model.setSeriesMarker("Eukarya", true);

        model.addData(hashToArray(hash_a, hash_length_a), "Archaea");

        model.setSeriesLine("Archaea", false);
        model.setSeriesMarker("Archaea", true);

        model.addData(hashToArray(hash_b, hash_length_b), "Bacteria");

        model.setSeriesLine("Bacteria", false);
        model.setSeriesMarker("Bacteria", true);

        CoordSystem coord = new CoordSystem(model);
        coord.setXAxisUnit("Longueur moyenne de la séquence");
        coord.setYAxisUnit("Nombre d'apparitions de l'anticodon");

        ChartPanel chartPanel = new ChartPanel(model,"Nombre d'apparitions de l'anticodon / longueur moyenne de la séquence");

        chartPanel.setCoordSystem(coord);

        MultiScatterChartRenderer renderer = new MultiScatterChartRenderer(coord,model);
        renderer.setAllowBuffer(false);

        chartPanel.addChartRenderer(renderer, 0);

        jp_charts = new JPanel(new BorderLayout());
        jp_charts.add(chartPanel);
    }
    private double[][] hashToArray(Hashtable<String, Integer> first, Hashtable<String, Double> second) {
        double[][] data = new double[first.size()][2];

        Enumeration<String> keys = first.keys();
        Enumeration<Integer> elem = first.elements();

        int j = 0;
        while(keys.hasMoreElements() && elem.hasMoreElements()) {
            Integer tmp = elem.nextElement();
            String tmp_name = keys.nextElement();

            Double length = second.get(tmp_name);

            data[j][0] = length;
            data[j][1] = tmp;

            j++;
        }

        return data;
    }
	private double[][] getRandomSeries(int length) {
		double[][] return_val = new double[length][2];

		for(int i=0;i<length;i++) {

			// Scale some random values to be between 0 and 100
			return_val[i][0] = Math.random()*100.0;
			return_val[i][1] = Math.random()*100.0;
		}

		return return_val;
	}

    public JPanel getPanel() {
        return jp_charts;
    }
}

